import {
    generateRandomId,
    generateClassToId,
    getDom,
    getDomList,
    addErrorClass,
    InputType,
    StrategyKey
} from '@/utils';
import type { BaseTableConfig } from '@/components/base-ui/base-table/type';
import type { StrategyParams } from '@/utils';

/**
 * 生产节拍，加工批量校验
 */
function getRuleFn(key: string, id: string) {
    return (params: StrategyParams) => {
        if (params.value) {
            const filterData = (params.value as Array<any>).filter((item: any) => !item[key])[0];

            if (filterData) {
                const validErrIndex = (params.value as Array<any>).findIndex(
                    (item: anyObj) => !item[key]
                );

                const tableBodyEl = getDom('.el-table__body', params.tableInstance.$el);
                const tableRowEl = getDomList('.el-table__row', tableBodyEl!)?.[params.row!];
                const validErrEl = getDomList(`.${generateClassToId(id)}`, tableRowEl)?.[
                    validErrIndex
                ];

                addErrorClass(validErrEl!);

                return '请完善必填信息';
            }
        }
    };
}

export const modalTableConfig: BaseTableConfig = {
    items: [
        {
            tableSlot: 'index'
        },
        {
            modelKey: 'processCode',
            slotName: 'processCode',
            columnProps: {
                label: '工序编码',
                width: 160
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            modelKey: 'processName',
            slotName: 'processName',
            columnProps: {
                label: '工序名称',
                width: 160
            },
            elInputProps: {
                disabled: true
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            itemType: 'number',
            modelKey: 'replacementTime',
            default: 0,
            columnProps: {
                label: '换型时间（S）',
                width: 160
            },
            elInputNumberProps: {
                min: 0,
                max: 9999999,
                precision: 0
            },
            validProps: {
                id: generateRandomId()
            },
            validFormatType: InputType.NUMBER
        },
        {
            itemType: 'number',
            modelKey: 'transshipmentBatch',
            default: 1,
            columnProps: {
                label: '转运批量',
                width: 160
            },
            elInputNumberProps: {
                min: 1,
                max: 9999999999,
                precision: 0
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            itemType: 'number',
            modelKey: 'transshipmentTime',
            default: 1,
            columnProps: {
                label: '转运时间（S）',
                width: 160
            },
            elInputNumberProps: {
                min: 1,
                max: 9999999,
                precision: 0
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            modelKey: 'processingContent',
            columnProps: {
                label: '加工内容',
                width: 240
            },
            elInputProps: {
                type: 'textarea',
                maxlength: '50',
                showWordLimit: true
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            modelKey: 'workingProcedureEquipments',
            slotName: 'workingProcedureEquipments',
            noEdit: true,
            columnProps: {
                label: '加工设备',
                width: 160
            },
            elInputProps: {
                disabled: true
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            modelKey: 'workingProcedureAuxiliary',
            slotName: 'workingProcedureAuxiliary',
            noEdit: true,
            columnProps: {
                label: '辅助资源',
                width: 160
            },
            elInputProps: {
                disabled: true
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            modelKey: 'processEquipments',
            slotName: 'workingProcedureList1',
            noEdit: true,
            columnProps: {
                label: '加工批量',
                width: 160
            },
            elInputNumberProps: {
                min: 1,
                max: 9999999999,
                precision: 0
            },
            validProps: {
                id: generateRandomId(),
                required: true
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('processingBatch', '1'),
                    errMsgFn: (row: number) => {
                        return `请完善工序号为 ${row} 的加工批量信息`;
                    }
                }
            ]
        },
        {
            modelKey: 'processEquipments',
            slotName: 'workingProcedureList2',
            noEdit: true,
            columnProps: {
                label: '生产节拍（S）',
                width: 160
            },
            elInputNumberProps: {
                min: 1,
                max: 9999999,
                precision: 0
            },
            validProps: {
                id: generateRandomId(),
                required: true
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('productionBeat', '2'),
                    errMsgFn: (row: number) => {
                        return `请完善工序号为 ${row} 的生产节拍信息`;
                    }
                }
            ]
        }
    ],
    unIndex: true,
    unAction: true
    // expand: true
};
